package com.ym.task;



import com.ym.core.redis.RedisKeyEnum;
import com.ym.core.redis.RedisUtil;
import com.ym.medical.domain.entity.HospitalEntity;
import com.ym.medical.service.IHospitalSV;

import net.javacrumbs.shedlock.core.SchedulerLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


import java.util.Map;


/**
 * 定时任务
 *
 * @author: wenbo
 * @version: 2020年03月23日 14:24
 */
@Component
public class ScheduledTask {
	private static final String THIRTY_MIN = "PT30M";

	private static Logger log = LoggerFactory.getLogger(ScheduledTask.class);

	@Autowired
	private IHospitalSV iHospitalSV;

	@Autowired
	private RedisUtil redisUtil;



	/***
	 *医院详情接口 点击量入库操作  每天每两小时执行一次
	 *0 0/5 * * * ?
	 * 0 0 0/2 * * ? *
	 */
	@Scheduled(cron = "0 0 0/2 * * ?")
	@SchedulerLock(name = "hospitalHeatSearchTask", lockAtMostForString = "PT20S", lockAtLeastForString = "PT10S")
	public void hospitalHeatSearchTask() {
		log.info("==================>>>>>>医院详情接口 点击量入库操作===========>执行");
		String hashKey = RedisKeyEnum.HOSPITAL_CLICK_ON_INC.build("increase");
		Map<Object, Object> maps = redisUtil.hmget(hashKey);
		if (maps.isEmpty()) {
			log.info("==================>>>>>>医院详情接口 点击量入库操作 缓存为空");
			return;
		}
		int mapSize = maps.size();
		//递增的key
		String[] increKeys = new String[mapSize];
		HospitalEntity hospital = null;
		for (Map.Entry<Object, Object> entry : maps.entrySet()) {
			hospital = new HospitalEntity();
			hospital.setId(Long.valueOf(String.valueOf(entry.getKey())));
			hospital.setHeatSearch(Long.valueOf(String.valueOf(entry.getValue())));
			String increKey = RedisKeyEnum.HOSPITAL_CLICK_ON_INC.build(String.valueOf(entry.getKey()));
			increKeys[mapSize - 1] = increKey;
			mapSize--;
			iHospitalSV.updateById(hospital);
		}

		//删除缓存 递增key  mapKey
		redisUtil.del(increKeys);
		redisUtil.del(hashKey);
		log.info("==================>>>>>>医院详情接口 点击量入库操作============>成功");
	}



}
